var signalResponders = new Array();



function sendSignal ( signalString, message ) {
	var responders = findRespondersForSignal ( signalString );
	for(i=0;i<responders.length;i++){
		var r=responders[i];
		r.responder(message);
	}
}

function registerResponderForSignal ( signalString, responseFunc ) {
	// use timestamp as id for now
	var thisId = Math.round((new Date().getTime())/1000);
	var responder = { id: thisId, signal: signalString, responder: responseFunc };
	signalResponders.push( responder );
	return thisId;
}


function removeResponderForSignal ( responderId ) {
	var responder = findResponderById ( responderId );
	if ( responder != null ) {
		var index = signalResponders.indexOf ( responder );
		signalResponders.slice(index, index+1);
	}
}



function findResponderById ( id ) {
	var responder = null;
	for (var i=0;i<signalResponders.length;i++){
		var r = signalResponders[i];
		if ( r.id == responderId ) {
			responder = r;
		}
	}
	return responder;
}



function findRespondersForSignal ( signalString ) {
	var responders = new Array();
	for (var i=0;i<signalResponders.length;i++) {
		var r = signalResponders[i];
		if ( r.signal == signalString ) {
			responders.push(r);
		}
	}
	return responders;
}